An Introduction to R and Python For Data Analysis: A Side-By-Side Approach by Brown Taylor R
Author:Brown, Taylor R.
Language: eng
Format: epub
Publisher: CRC Press
Published: 2023-04-10T00:00:00+00:00
9.5âSaving Data in Python
9.5.1âWriting Out Tabular Plain Text Data in Python
You can write out tabular data with a variety of DataFrame methods that are named to_*().12. pd.DataFrame.to_csv()13 has a lot of common with write.csv() in R. Below we write out d to a file called oring_out2.csv.
Here is how the first few rows of that file looks in a text editor.
9.5.2âSerialization in Python
Serialization functionality is readily available in Python, just like it is in R. In Python, the pickle14 and cPickle libraries are probably the most commonly used. Serializing objects with these libraries is known as pickling an object.
Pandas has a .to_pickle()15 wrapper method attached to every DataFrame. Once the pickled object is saved, the file can be read back into Python with pd.read_pickle()16. These functions are extremely convenient, because they call all the required pickle code and hide a decent amount of complexity.
Here is an example of writing out d and then reading the pickled object back in. In Python 3, the file suffix for pickled objects is usually .pickle, but there are many other choices.
__________________
â12https://pandas.pydata.org/pandas-docs/stable/reference/io.html#input-output 13https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html#pandas.DataFrame.to_csv 14https://docs.python.org/3/library/pickle.html 15https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_pickle.html 16https://pandas.pydata.org/docs/reference/api/pandas.read_pickle.html#pandas.read_pickle
Unfortunately, "oring.pickle" is much larger (1,676 bytes) than the original text file "o-ring-erosion-only.data" (322 bytes). This is for two reasons. First, the original data set is small, so the overhead of pickling this object is relatively pronounced, and second, we are not taking advantage of any compression. If you use something like d_is_back.to_pickle("data/oring.zip") it will become smaller.
In Python, unlike in R, it is more difficult to serialize all of the objects you currently have in memory. It is possible, but it will likely require the use of a third-party library.
Speaking of third-party code, there are many that provide alternative serialization solutions in both R and Python. I do not discuss any in this text. However, I will mention that some of them may provide combinations of the following: an increase in read and write speed, a decrease in required memory, improved security17, improved human readability and interoperability between multiple programming languages. If any of these sound potentially beneficial, I encourage you to conduct further research.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Ada | Ajax |
Assembly Language Programming | Borland Delphi |
C & C++ | C# |
CSS | Compiler Design |
Compilers | DHTML |
Debugging | Delphi |
Fortran | Java |
Lisp | Perl |
Prolog | Python |
RPG | Ruby |
Swift | Visual Basic |
XHTML | XML |
XSL |
Hello! Python by Anthony Briggs(9867)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9757)
The Mikado Method by Ola Ellnestam Daniel Brolund(9747)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8258)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7745)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7741)
Grails in Action by Glen Smith Peter Ledbrook(7667)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7517)
Windows APT Warfare by Sheng-Hao Ma(6502)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6378)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6248)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6117)
Kotlin in Action by Dmitry Jemerov(5019)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4297)
Functional Programming in JavaScript by Mantyla Dan(4018)
Solidity Programming Essentials by Ritesh Modi(3839)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3614)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3566)
The Ultimate iOS Interview Playbook by Avi Tsadok(3532)
